package function

import (
	"dev.ixmxm.top/rms/utils/log"
	"reflect"
)

//三目运算符 interface
func TernaryOperatorInterface(condition bool, trueValue, falseValue interface{}) interface{} {
	if condition {
		return trueValue
	}
	return falseValue
}

//三目运算符 string
func TernaryOperatorString(condition bool, trueValue, falseValue string) string {
	if condition {
		return trueValue
	}
	return falseValue
}

//三目运算符 int
func TernaryOperatorInt(condition bool, trueValue, falseValue int) int {
	if condition {
		return trueValue
	}
	return falseValue
}

//三目运算符 int64
func TernaryOperatorInt64(condition bool, trueValue, falseValue int64) int64 {
	if condition {
		return trueValue
	}
	return falseValue
}

//三目运算符 uint64
func TernaryOperatorUInt64(condition bool, trueValue, falseValue uint64) uint64 {
	if condition {
		return trueValue
	}
	return falseValue
}

//三目运算符 float
func TernaryOperatorFloat32(condition bool, trueValue, falseValue float32) float32 {
	if condition {
		return trueValue
	}
	return falseValue
}

//选择输出 string
func SelectString(condition int, values ...string) string {
	//Assert(condition>=0 && condition <= len(values)-1,fmt.Sprintf("input condition is invalid:condition[%d], len[%d]", condition, len(values)))
	return values[condition]
}

type Equal interface {
	Equal(v interface{}) bool
}

func SliceUnique(s interface{}) bool {
	t := reflect.ValueOf(s)
	if t.Type().Kind() != reflect.Ptr {
		log.Error("unique input need to be ptr")
		return false
	}
	slice := t.Elem()
	if t.Elem().Kind() != reflect.Slice {
		log.Error("unique input need to be slice")
		return false
	}
	newResult := reflect.New(t.Elem().Type()).Elem()
	for i := 0; i < slice.Len(); i++ {
		found := false
		for j := 0; j < newResult.Len(); j++ {
			if reflect.DeepEqual(slice.Index(i).Interface(), newResult.Index(j).Interface()) {
				found = true
				break
			}
		}
		if !found {
			newResult = reflect.Append(newResult, slice.Index(i))
		}
	}
	t.Elem().Set(newResult)
	return true
}
